<template>
    <el-upload class="upload"
               :action="uploadApi"
               :accept="fileType"
               :show-file-list="false"
               :before-upload="beforeUpload"
               :on-success="uploadSuccess"
               :headers="headers"
               :data="sendData"
               v-if="btnDisplay">
      <auth-button v-if="btnDisplay" :size="btnSize" icon="iconfont iconshangchuan" type="primary" :label="label" :menuCode="menuCode" :perms="perms"></auth-button>
    </el-upload>
</template>

<script>
  import AuthButton from '@/components/auth/authButton'
  import httpConfig from '@/../config/httpConfig'
  import { getHeaders } from '@/utils/devUtils'
  export default {
    components: {AuthButton},
    props: {
      // 所属菜单Code-->全局唯一
      menuCode: {
        type: String,
        default: null
      },
      // 按钮权限标识，外部使用者传入
      perms: {
        type: String,
        default: null
      },
      // 文件ID
      fileID: {
        type: [String, Number],
        default: function () {
          return null
        }
      },
      // 文件名
      fileName: {
        type: String,
        default: function () {
          return null
        }
      },
      // 所属类型CODE
      belongCode: {
        type: String,
        // required: true,
        default: function () {
          return 'GYS'
        }
      },
      // 所属业务ID
      belongID: {
        type: String,
        // required: true,
        default: function () {
          return null
        }
      },
      // 限制文件类型
      fileType: {
        type: String,
        default: function () {
          return '.xlsx'
        }
      },
      // 限制文件大小(M)
      fileSize: {
        type: Number,
        default: function () {
          return 50
        }
      },
      // 文件管理服务上传地址
      uploadApi: {
        type: String,
        // required: true,
        default: function () {
          return httpConfig.MID_FILE + '/api/file/file/parsing/excel'
        }
      },
      // 按钮大小
      btnSize: {
        type: String,
        default: function () {
          return ''
        }
      },
      btnDisplay: {
        type: Boolean,
        default: function () {
          return false
        }
      },
      // 上传按钮显示内容
      uploadName: {
        type: String,
        default: function () {
          return this.$i18n.messages[this.$i18n.locale].common.upload
        }
      },
      // 重新上传按钮显示内容
      reUploadName: {
        type: String,
        default: function () {
          return this.$i18n.messages[this.$i18n.locale].common.reUpload
        }
      }
    },
    data () {
      return {
        fullscreenLoading: false,
        newFileName: this.fileName,
        headers: getHeaders()
      }
    },
    computed: {
      sendData: function () {
        return {
          belongCode: this.belongCode,
          belongID: this.belongID
        }
      },
      currentFileId: {
        get: function () {
          return this.fileID
        },
        set: function (val) {
          return this.$emit('update:fileID', val)
        }
      },
      label () {
        return this.UtilJs.isEmpty(this.currentFileId) ? this.uploadName : this.reUploadName
      }
    },
    watch: {
    },
    methods: {
      beforeUpload (file) {
        if (!this.UtilJs.isNull(this.fileSize) && file.size / 1024 > this.fileSize * 1024) {
          this.warning(`已超出上传文件最大限制${this.fileSize}M`)
          return false
        }
        var fileType = '.' + file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase()
        var banFileTypes = ['.exe', '.bat', '.cmd', '.sh', '.py']
        var banFileTypeFlag = 0
        banFileTypes.forEach(item => {
          if (item === fileType) {
            banFileTypeFlag = 1
          }
        })
        if (banFileTypeFlag === 1) {
          this.warning('不允许上传' + fileType.substring(fileType.lastIndexOf('.') + 1) + '文件')
          return false
        }
        if (!this.UtilJs.isNull(this.fileType)) {
          var fileTypeFlag = 0
          var fileTypeList = this.fileType.toLowerCase().split(',')
          fileTypeList.forEach(it => {
            if (fileType === it) {
              fileTypeFlag = 1
            }
          })
          if (fileTypeFlag === 0) {
            this.warning('只能上传' + this.fileType.toLowerCase().replace(/\./g, '') + '文件')
            return false
          }
        }
        let returnData = {}
        returnData.rowData = this.rowData
        returnData.rowIndex = this.rowIndex
        returnData.index = this.index
        this.$emit('beforeUpload', returnData)
        this.fullscreenLoading = this.$loading({
          lock: true,
          text: '导入中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        return true
      },
      uploadSuccess (response) {
        if (response.code !== '0') {
          this.fullscreenLoading.close()
          this.error(response.message)
          return
        }
        this.$emit('importSuccess', response.data)
        this.fullscreenLoading.close()
      }
    }
  }
</script>

<style scoped>
  .upload{
    display:inline-block;
  }
</style>
